//
//  CALoginViewController.m
//  qiche
//
//  Created by haley liu on 12-12-17.
//  Copyright (c) 2012年 iCar. All rights reserved.
//

#import "CALoginViewController.h"
#import "CAAppDelegate.h"
#import "CAObject.h"
#import "CAWeiboObject.h"

@interface CALoginViewController ()

@end

@implementation CALoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)loginAction:(id)sender
{
    [[CAAppDelegate globalAppdelegate] initView];
    return;
    NSString *accountStr = [NSString stringWithString:_accountView.text];
    NSString *pwdStr = [NSString stringWithString:_pwdView.text];
    if ([accountStr length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"请输入您的账号!"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
        return;
    }
    if ([pwdStr length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"请输入您的密码!"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
        return;
    }
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:accountStr forKey:@"uName"];
    [dict setValue:pwdStr forKey:@"pwd"];
    [CNetAgentCenter loginwithParam:dict OnTarget:self andMethodToExecuting:@selector(loginResult:)];
    [dict release];
    
    [[LKLoadingCenter defaultCenter] postLoadingWithTitle:@"登录中..." message:nil ignoreTouch: YES];
}

#pragma mark - touch event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    if (!CGRectContainsPoint(_accountView.frame, point) && !CGRectContainsPoint(_pwdView.frame, point)) {
        if ([_accountView isFirstResponder])
            [_accountView resignFirstResponder];
        if ([_pwdView isFirstResponder])
            [_pwdView resignFirstResponder];
    }
}


#pragma mark- UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{    
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
	return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == _accountView) {
        [_pwdView becomeFirstResponder];
    } else if (textField == _pwdView) {
        if ([_pwdView isFirstResponder])
            [_pwdView resignFirstResponder];
        [self loginAction:nil];
    }
    
    return YES;
}

#pragma mark -
#pragma mark keyboard handle
//键盘弹出前通知回调方法
- (void)keyboardWillShow:(NSNotification *)noti
{
    return;
}

//键盘隐藏前通知回调方法
- (void)keyboardWillHide:(NSNotification *)noti {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    self.view.frame = CGRectMake(CGRectGetMinX(self.view.frame),
                                 0,
                                 CGRectGetWidth(self.view.frame),
                                 CGRectGetHeight(self.view.frame));
    [UIView commitAnimations];
    
    return;
}

-(void)loginResult:(id)sender
{
    if ([sender isKindOfClass:[NSDictionary class]])
    {
        NSDictionary* dic = (NSDictionary*)sender;
        if (dic)
        {
            NSString *err = [dic valueForKey:NETERR];
            if (err && [err length] > 0)
            {
                [[LKLoadingCenter defaultCenter] disposeLoadingView];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                                message:err
                                                               delegate:nil
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles: nil];
                [alert show];
                [alert release];
                return;
            }
            //parse data
            [CAAppDelegate globalAppdelegate].userName = [dic objectForKey:@"userName"];
            
            if ([[dic objectForKey:@"vins"] isKindOfClass:[NSArray class]]) {
                NSArray *vinList = (NSArray *)[dic objectForKey:@"vins"];
                NSMutableArray *vinList1 = [[NSMutableArray alloc] init];
                for (id obj in vinList) {
                    if ([obj isKindOfClass:[NSDictionary class]]) {
                        CAVinObject *vinObject = [[CAVinObject alloc] initWithDict:(NSDictionary *)obj];
                        [vinList1 addObject:vinObject];
                        [vinObject release];
                    }
                }

                [CAAppDelegate globalAppdelegate].carVinList = vinList1;
                [vinList1 release];
            }
            
            if ([[dic objectForKey:@"Weibo"] isKindOfClass:[NSArray class]]) {
                NSArray *weiboList = (NSArray *)[dic objectForKey:@"Weibo"];
                NSMutableArray *weiboList1 = [[NSMutableArray alloc] init];
                for (id obj in weiboList) {
                    if ([obj isKindOfClass:[NSDictionary class]]) {
                        CAWeiboObject *object = [[CAWeiboObject alloc] initWithDict:(NSDictionary *)obj];
                        [weiboList1 addObject:object];
                        [object release];
                    }
                }
                
                [CAAppDelegate globalAppdelegate].weiboObjectList = weiboList1;
                [weiboList1 release];
            }
            [[CAAppDelegate globalAppdelegate] initView];
        }
    }
    [[LKLoadingCenter defaultCenter] disposeLoadingView];
}
@end
